﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using Tao.OpenGl;
using Tao.Platform;
using Tao.FreeGlut;



namespace testOpenGL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            int width = 500;
            int height = 572;
            InitializeComponent();
            this.simpleOpenGlControl1.InitializeContexts();
            Gl.glClearDepth(1.0);
            Gl.glClearColor(0, 0, 0, 0);

            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glDepthMask(Gl.GL_TRUE);

            Gl.glViewport(0, 0, width, height);

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();

            Glu.gluPerspective(40.0f, (double)width / (double)height, 0.01f, 5000.0f);
            Gl.glEnable(Gl.GL_CULL_FACE);
            Gl.glCullFace(Gl.GL_BACK);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.simpleOpenGlControl1.Invalidate();
        }

        private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();

            
            
            Gl.glTranslatef(0, 0, -10);
            Gl.glColor3f(1, 0, 0);
            
            Drawcircle(360,2);
            
            Gl.glEnd();
            
        }

        void Drawcircle(int Seg, int rad)
        {
            int i;
            float[] Px = new float[Seg];  // mang 1 chieu Px
            float[] Py = new float[Seg];  // mang 1 chieu Py
            float AngIncr = (2.0f * 3.1415927f) / (float)Seg;
            float Ang = AngIncr;
            Px[0] = 1;

            Py[0] = 0;

            for (i = 1; i < Seg; i++, Ang += AngIncr)
            {
                Px[i] = rad*(float)Math.Cos(Ang);
                Py[i] = rad*(float)Math.Sin(Ang);
            }
                Gl.glBegin(Gl.GL_LINE_LOOP);
                //Gl.glVertex3f(0, 1, 0);
                for (i = 0; i < Seg; i++)
                    Gl.glVertex3f(Px[i], -Py[i],0 );
                Gl.glVertex3f(Px[0],-Py[0] , 0 );
                Gl.glEnd();
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        
        private void BConnect_Click(object sender, EventArgs e)
        {
            if (LbStatus.Text == "DISCONNECT")
            {
                Com.PortName = CbSetCom.Text;
                Com.Open();
                LbStatus.Text = "CONNECT";
                BConnect.Text = "DISCONNECT";
                PStatus.Value = 100;
            }
            else
            {
                Com.Close();
                LbStatus.Text = "DISCONNECT";
                BConnect.Text = "CONNECT";
                PStatus.Value = 0;
            }
        }

        int intlen = 0;

        private void timer2_Tick(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            if (intlen != ports.Length)
            {
                intlen = ports.Length;
                CbSetCom.Items.Clear();
                for (int j = 0; j < intlen; j++)
                {
                    CbSetCom.Items.Add(ports[j]);
                }

            }
        }
        public void SendCommand(string s)
        {
            Com.WriteLine(s);
        }

        int ti = 10;

        private void t(object sender, MouseEventArgs e)
        {
            int s;
            s = ti + 10;
            SendCommand(s.ToString());
        }

        private void test(object sender, MouseEventArgs e)
        {
            int s;
            s = ti - 10;
            SendCommand(s.ToString());
        }

                 
    }
}
